// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   ParaManage.java

package com.vandagroup.common.treedic;

import java.io.*;
import java.net.URL;
import java.util.Properties;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class ParaManage
{

    public static final String strParaFileName = System.getProperty("user.dir") + System.getProperty("file.separator") + "EHub.ini";
    public static final String strParaXMLFileName = getXMLCONFPath();
    public static Properties aaa = null;

    public ParaManage()
    {
    }

    public static String getPara(String paKey)
    {
        String strKeyValue = "";
        try
        {
            if(aaa == null)
            {
                FileInputStream in = new FileInputStream(strParaFileName);
                aaa = new Properties();
                aaa.load(in);
                in.close();
            }
            strKeyValue = aaa.getProperty(paKey, "");
        }
        catch(Exception exception) { }
        return strKeyValue;
    }

    public static String getPara(String paKey, String defautlValue)
    {
        String strKeyValue = "";
        try
        {
            if(aaa == null)
            {
                FileInputStream in = new FileInputStream(strParaFileName);
                aaa = new Properties();
                aaa.load(in);
                in.close();
            }
            strKeyValue = aaa.getProperty(paKey, defautlValue);
        }
        catch(Exception exception) { }
        return strKeyValue;
    }

    public static boolean setPara(String paKey, String paValues)
    {
        try
        {
            if(aaa == null)
            {
                aaa = new Properties();
                FileInputStream in = new FileInputStream(strParaFileName);
                aaa.load(in);
                in.close();
            }
            aaa.setProperty(paKey, paValues);
            FileOutputStream out = new FileOutputStream(strParaFileName);
            aaa.store(out, "ESS Parameter Management");
            out.close();
            return true;
        }
        catch(Exception e)
        {
            return false;
        }
    }

    public static boolean writeObject(String paFileName, Object paObj)
    {
        boolean bRslt = false;
        try
        {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(paFileName));
            out.writeObject(paObj);
            out.close();
            bRslt = true;
        }
        catch(Exception exception) { }
        return bRslt;
    }

    public static Object readObject(String paFileName)
    {
        Object obj = null;
        try
        {
            FileInputStream istream = new FileInputStream(paFileName);
            ObjectInputStream in = new ObjectInputStream(istream);
            obj = in.readObject();
            in.close();
            istream.close();
        }
        catch(Exception exception) { }
        return obj;
    }

    public static String getParaXML(String parentTagName, String currTagName, String noteName, String defautlValue)
    {
        String strNoteNameValue;
        strNoteNameValue = "";
        try
        {
            InputStream is = null;
            try
            {
                is = (new URL(strParaXMLFileName)).openStream();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            DocumentBuilderFactory caf = DocumentBuilderFactory.newInstance();
            caf.setNamespaceAware(true);
            DocumentBuilder cadoc = null;
            try
            {
                cadoc = caf.newDocumentBuilder();
            }
            catch(ParserConfigurationException e)
            {
                e.printStackTrace();
            }
            Document doc = cadoc.parse(is);
            Element root = (Element)doc.getElementsByTagName(parentTagName).item(0);
            NodeList currNodeList = root.getElementsByTagName(currTagName);
            strNoteNameValue = getNodeListToStr(currNodeList, noteName);
            is.close();
        }
        catch(Exception exception) { }
        finally
        {
            if(strNoteNameValue == null || strNoteNameValue.equals(""))
            {
                if(defautlValue == null)
                    defautlValue = "";
                strNoteNameValue = defautlValue;
            }
        }
        return strNoteNameValue;
    }

    public static String getNodeListToStr(NodeList xmlNodeList, String attributeName)
    {
        String xmlNodeText = "";
        try
        {
            if(xmlNodeList != null && xmlNodeList.getLength() > 0)
            {
                for(int i = 0; i < xmlNodeList.getLength(); i++)
                {
                    Element currxmlElement = (Element)xmlNodeList.item(i);
                    if(!currxmlElement.getAttribute("name").trim().equals(attributeName))
                        continue;
                    xmlNodeText = currxmlElement.getAttribute("value").trim();
                    break;
                }

            } else
            {
                xmlNodeText = "";
            }
        }
        catch(Exception e)
        {
            xmlNodeText = "";
        }
        return xmlNodeText;
    }

    public static String getXMLCONFPath()
    {
        String strParaXMLFilePath = "";
        try
        {
            String path = "file:///" + System.getProperty("user.dir") + System.getProperty("file.separator") + "includes/systemconf.xml";
            strParaXMLFilePath = path.replace('\\', '/');
        }
        catch(Exception e)
        {
            strParaXMLFilePath = "";
        }
        return strParaXMLFilePath;
    }

}
